//
//  KFDFAQViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/5/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDFAQViewController.h"
#import "KFDFAQViewModel.h"
#import "KFDUIConfigurator.h"
#import "KFDFAQCellContentComposer.h"

@interface KFDFAQViewController ()

@property (strong,nonatomic) KFDFAQViewModel* viewModel;

@end

@implementation KFDFAQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"帮助中心";
    self.viewModel=[[KFDFAQViewModel alloc] init];
    [self.viewModel loadFaqsWithCompletionHandler:^(BOOL success) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }];
}

-(void)faqsReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.faqs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    KFDFAQViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KFDFAQViewCell" forIndexPath:indexPath];
//    cell.faq=self.viewModel.faqs[indexPath.row];
//    return cell;
    UITableViewCell* cell=[[UITableViewCell alloc] init];
    KFDFAQCellContentComposer* composer=[[KFDFAQCellContentComposer alloc] init];
    composer.faq=self.viewModel.faqs[indexPath.row];
    [composer layout];
    [cell.contentView addSubview:composer.view];
    return cell;
}

-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    KFDFAQCellContentComposer* composer=[[KFDFAQCellContentComposer alloc] init];
    composer.faq=self.viewModel.faqs[indexPath.row];
    return [composer layout];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
